package com.five.api.controller;

import com.five.controller.BaseController;
import com.five.api.service.StudyTraceService;
import com.five.entity.Orders;
import com.five.entity.StudyTrace;
import com.five.entity.User;
import com.five.qo.PageQO;
import com.five.util.MySessionContext;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:21
 *
 * 学习足迹 Controller层
 */
@Controller
@RequestMapping("studyTrace")
@CrossOrigin
@Api(tags = "学习足迹业务接口")
public class StudyTraceController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(StudyTraceController.class);

    @Autowired
    private StudyTraceService studyTraceService;


    @ResponseBody
    @RequestMapping(value = "selectStudyTraces",method = RequestMethod.POST)
    @ApiOperation(value = "查询用户学习足迹接口" )
    public BaseResultVO<StudyTrace> selectStudyTrace(@ApiParam("用户id") @RequestParam("id") Long id){

        List<StudyTrace> studyTraces = studyTraceService.selectStudyTraces(id);
        logger.info("id= "+id+"，用户查询学习足迹："+studyTraces);
        if (studyTraces == null){
            return errorResp("当前用户没有学习足迹");
        }else {
            return successResp(studyTraces);
        }
    }

    @ResponseBody
    @RequestMapping(value = "selectStudyTraceByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询用户学习足迹接口" )
    public BaseResultVO<StudyTrace> selectStudyTraceByPage(@ApiParam("分页对象") @Param("pageQO") PageQO pageQO){
        System.out.println("订单号："+pageQO);
        List<StudyTrace> studyTraces = studyTraceService.selectStudyTracesPage(pageQO);
        logger.info("分页对象= "+pageQO+"，用户分页查询学习足迹："+studyTraces);
        if (studyTraces == null){
            return errorResp("当前用户没有学习足迹");
        }else {
            return successResp(studyTraces);
        }
    }

    @ResponseBody
    @RequestMapping(value = "insertStudyTrace", method = RequestMethod.POST)
    @ApiOperation(value = "更新课程足迹")
    public BaseResultVO<Orders> updateViewCount(@ApiParam("课程足迹bean") StudyTrace studyTrace, HttpServletRequest request){

        User sessionUser = (User) SecurityUtils.getSubject().getPrincipal();
        if(sessionUser == null){
            return errorResp("用户未登录不需要更新足迹");
        }
        studyTrace.setUserId(sessionUser.getId());
        int flag = studyTraceService.insertStudyTracesPage(studyTrace);
        if(flag == 1){
            return successRespWithMessage("学习足迹更新成功");
        }
        return errorResp("学习足迹更新失败");
    }
}
